package com.Animations;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import com.shareup.R;


public class AnimUtil {

    private static boolean callbackWasCalled;

    @SuppressWarnings("deprecation")
    public static void AnimateDrawablesIn(View v, Drawable in) {

        Drawable[] cambios = new Drawable[2];
        cambios[0] = new ColorDrawable(Color.TRANSPARENT);
        cambios[1] = in;

        TransitionDrawable trans = new TransitionDrawable(cambios);
        trans.setCrossFadeEnabled(true);
        v.setBackgroundDrawable(trans);

        trans.startTransition(400);
    }

    public static void AnimateDrawablesInImage(ImageView v, Drawable in) {

        Drawable[] cambios = new Drawable[2];
        cambios[0] = new ColorDrawable(Color.TRANSPARENT);
        cambios[1] = in;

        TransitionDrawable trans = new TransitionDrawable(cambios);
        trans.setCrossFadeEnabled(true);
        v.setImageDrawable(trans);

        trans.startTransition(400);
    }

    @SuppressWarnings("deprecation")
    public static void AnimateDrawablesOut(View v, Drawable out) {

        Drawable[] cambios = new Drawable[2];
        cambios[0] = v.getBackground();
        cambios[1] = out;

        TransitionDrawable trans = new TransitionDrawable(cambios);
        trans.setCrossFadeEnabled(true);
        v.setBackgroundDrawable(trans);

        trans.startTransition(400);
    }

    public static void AnimateDrawablesOutImage(ImageView v) {

        Drawable[] cambios = new Drawable[2];
        cambios[0] = v.getDrawable();
        cambios[1] = new ColorDrawable(Color.TRANSPARENT);

        TransitionDrawable trans = new TransitionDrawable(cambios);

        trans.setCrossFadeEnabled(true);
        v.setImageDrawable(trans);

        trans.startTransition(400);
    }

    public static void AnimateDrawablesOutImage(ImageView v, Drawable out) {

        Drawable[] cambios = new Drawable[2];
        cambios[0] = v.getDrawable();
        cambios[1] = out;

        TransitionDrawable trans = new TransitionDrawable(cambios);
        trans.setCrossFadeEnabled(true);
        v.setImageDrawable(trans);

        trans.startTransition(400);
    }

    public static void animateFadeIn(final View v) {

        final Animation anim_in = AnimationUtils.loadAnimation(v.getContext(), android.R.anim.fade_in);
        anim_in.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {

                v.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationStart(Animation arg0) {

                // TODO Auto-generated method stub
            }
        });
        v.startAnimation(anim_in);
    }

    public static void animateFadeIn(final View v, final AnimationCompleted callback) {

        final Animation anim_in = AnimationUtils.loadAnimation(v.getContext(), android.R.anim.fade_in);
        anim_in.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {

                v.setVisibility(View.VISIBLE);

                if (callback != null)
                    callback.Completed();
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationStart(Animation arg0) {

                // TODO Auto-generated method stub
            }
        });
        v.startAnimation(anim_in);
    }

    /**
     * @param coverB
     */
    public static void animateFadeOut(final View v) {

        final Animation anim_out = AnimationUtils.loadAnimation(v.getContext(), android.R.anim.fade_out);
        anim_out.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {

                v.setVisibility(View.INVISIBLE);

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationStart(Animation arg0) {

                // TODO Auto-generated method stub
            }
        });
        v.startAnimation(anim_out);

    }

    public static void animateFadeOut(final View v, final AnimationCompleted callback) {

        final Animation anim_out = AnimationUtils.loadAnimation(v.getContext(), android.R.anim.fade_out);
        anim_out.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {

                v.setVisibility(View.INVISIBLE);

                if (callback != null)
                    callback.Completed();
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationStart(Animation arg0) {

                // TODO Auto-generated method stub
            }
        });
        v.startAnimation(anim_out);
    }

    public static void AnimateFadeOutIn(final View in, final boolean animate, final AnimationCompleted callback, final View... out) {

        final Animation anim_in = AnimationUtils.loadAnimation(out[0].getContext(), android.R.anim.fade_in);
        final Animation anim_out = AnimationUtils.loadAnimation(out[0].getContext(), android.R.anim.fade_out);
        callbackWasCalled = false;
        anim_out.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {

                if (animate) {
                    if (in != null) {
                        in.setVisibility(View.VISIBLE);
                        in.startAnimation(anim_in);
                    }

                }
                for (View v : out)
                    v.setVisibility(View.INVISIBLE);

                if (callback != null && !callbackWasCalled) {
                    callbackWasCalled = true;
                    callback.Completed();

                }

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationStart(Animation arg0) {

                // TODO Auto-generated method stub
            }
        });
        for (View v : out)
            v.startAnimation(anim_out);
    }

    public static void animatePictureChanges(ImageView imageView, Bitmap newImage) {

        Drawable bit = new BitmapDrawable(imageView.getContext().getResources(), newImage);
        if (imageView.getDrawable() == null)
            imageView.setImageDrawable(imageView.getResources().getDrawable(R.drawable.background_profile_picture));

        Drawable[] cambios = { imageView.getDrawable(), bit };
        imageView.setImageDrawable(null);
        TransitionDrawable trans = new TransitionDrawable(cambios);
        imageView.setImageDrawable(trans);
        trans.startTransition(400);
    }

    public static void animatePopUp(final ViewGroup error_container, boolean in, final AnimationCompleted callback) {

        final Animation anim_in = AnimationUtils.loadAnimation(error_container.getContext(), R.anim.push_up_in);
        //
        anim_in.setInterpolator(AnimationUtils.loadInterpolator(error_container.getContext(), android.R.anim.decelerate_interpolator));
        final Animation anim_out = AnimationUtils.loadAnimation(error_container.getContext(), R.anim.push_up_out);
        //
        anim_out.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {

                error_container.removeAllViews();
                if (callback != null)
                    callback.Completed();
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationStart(Animation arg0) {

                // TODO Auto-generated method stub
            }
        });
        if (in) {
            error_container.startAnimation(anim_in);
        }
        else {
            error_container.startAnimation(anim_out);
        }
    }

    public static void animatePopUpUP(final ViewGroup view, final boolean in, final AnimationCompleted callback) {

        final Animation anim_in = AnimationUtils.loadAnimation(view.getContext(), R.anim.push_up_in);
        //
        anim_in.setInterpolator(AnimationUtils.loadInterpolator(view.getContext(), android.R.anim.decelerate_interpolator));
        final Animation anim_out = AnimationUtils.loadAnimation(view.getContext(), R.anim.push_up_out);
        //
        anim_out.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {

                if (callback != null)
                    callback.Completed();

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationStart(Animation arg0) {

                if (in)
                    view.setVisibility(View.VISIBLE);
                else
                    view.setVisibility(View.INVISIBLE);
            }
        });
        if (in) {
            view.startAnimation(anim_in);
        }
        else {
            view.startAnimation(anim_out);
        }
    }

    // region ACTIVITY TRANSITIONS
    public static void getActivityTransition(Activity from, boolean in) {

        if (in) {
            from.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        }
        else {
            from.overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
        }
    }

    public static void PostIn(final Context c, final View v) {

        final Animation anim_in = AnimationUtils.loadAnimation(c, R.anim.push_left_in_post);
        anim_in.setInterpolator(new DecelerateInterpolator());
        v.startAnimation(anim_in);
    }

    public static void PostIn(final Context c, final View v, final AnimationCompleted callback) {

        final Animation anim_in = AnimationUtils.loadAnimation(c, R.anim.push_left_in_post);
        anim_in.setInterpolator(new DecelerateInterpolator());
        anim_in.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {

                v.setVisibility(View.VISIBLE);

                if (callback != null)
                    callback.Completed();

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation arg0) {

                // TODO Auto-generated method stub

            }
        });
        v.startAnimation(anim_in);
    }

    public static void PostOut(final Context c, final View v, final AnimationCompleted callback) {

        final Animation anim_out = AnimationUtils.loadAnimation(c, R.anim.push_left_out_post);
        anim_out.setInterpolator(new DecelerateInterpolator());
        anim_out.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {

                Log.e("animateout", "end...");
                v.setVisibility(View.INVISIBLE);

                if (callback != null) {
                    callback.Completed();
                    Log.e("animateout", "callback...");
                }

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation arg0) {

                Log.e("animateout", "start...");

            }
        });
        v.clearAnimation();
        v.startAnimation(anim_out);
        Log.e("animateout", "started...");
    }

    public static void AnimateFlyImage(final ImageView v, final int position, final Bitmap bit) {

        Animation anim = AnimationUtils.loadAnimation(v.getContext(), R.anim.flying_animation);
        anim.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {

                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation arg0) {

                if (position == (Integer) v.getTag()) {
                    v.setImageBitmap(bit);
                }

            }
        });

        v.startAnimation(anim);

    }

    public interface AnimationCompleted {

        void Completed();
    }

}
